﻿//公共共同脚本
var ApiUrl = "http://localhost:6260/";
var LoginUserID = "1";
var LoginUserName = "张三";
$(function () {
    //前台判断是否有令牌，否则直接跳转登录页面
    var storage = window.localStorage;
    var ticket = storage.getItem('token');
    if (ticket == null || ticket == "" || ticket == undefined) {
        location.href = "login.html";
    }
    else {
        var storage = window.localStorage;
        var userid = storage.getItem('userid');
        var account = storage.getItem('account');
        var name = storage.getItem('name');
        var pwd = storage.getItem('pwd');
        if (userid == null || userid == "" || userid == undefined ||
           account == null || account == "" || account == undefined ||
           name == null || name == "" || name == undefined ||
           pwd == null || pwd == "" || pwd == undefined) {
            location.href = "login.html";
        } else {
            LoginUserID = userid;
            LoginUserName = name;
        }
    }
})

//扩展
// var a = "我喜欢吃{0}，也喜欢吃{1}，但是最喜欢的还是{0},偶尔再买点{2}";
// alert(String.format(a, "苹果","香蕉","香梨"));
// 结果:我喜欢吃苹果，也喜欢吃香蕉，但是最喜欢的还是苹果,偶尔再买点香梨
String.format = function () {
    if(arguments.length == 0)
        return null;
    var str = arguments[0];
    for (var i = 1; i < arguments.length; i++) {
        var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
};
// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
// 例子： 
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
        };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 -RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]): (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
} 